import { createRouter, createWebHashHistory } from 'vue-router'
const Layouts = () => import('@/layouts/index.vue')
/**
 * 静态路由
 */
export const constantRoutes = [
  {
    path: '/',
    component: Layouts,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: {
          title: '首页',
          svgIcon: 'dashboard',
          affix: true
        }
      }
    ]
  },
  {
    path: '/button',
    component: Layouts,
    children: [
      {
        path: '/button',
        component: () => import('@/views/button/index.vue'),
        name: 'button',
        meta: {
          title: 'button'
        }
      }
    ]
  },
  {
    path: '/radio',
    component: Layouts,
    children: [
      {
        path: '/radio',
        component: () => import('@/views/radio/index.vue'),
        name: 'radio',
        meta: {
          title: 'radio'
        }
      }
    ]
  },
  {
    path: '/admin',
    component: Layouts,
    children: [
      {
        path: '/admin',
        component: () => import('@/views/admin/index.vue'),
        name: 'admin',
        meta: {
          title: 'admin'
        }
      }
    ]
  },
  {
    path: '/about',
    component: Layouts,
    children: [
      {
        path: '/about',
        component: () => import('@/views/about/index.vue'),
        name: 'about',
        meta: {
          title: 'about'
        }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: 'login', hidden: true }
  },
  {
    path: '/register',
    component: () => import('@/views/register/index.vue'),
    meta: { title: 'register', hidden: true }
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401.vue'),
    meta: { title: '401', hidden: true }
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404.vue'),
    meta: { title: '404', hidden: true }
  },
  {
    path: '/micro-app',
    name: 'micro-app',
    component: () => import('@/views/microApps/index.vue'),
    meta: { title: 'microApp' }
  },
  {
    // 👇 非严格匹配，/sub-app/* 都指向 my-page 页面
    path: '/sub-app',
    name: 'subApp',
    component: () => import('@/views/micro-page/index.vue'),
    meta: { title: 'subApp', hidden: true }
  }
]

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 })
})

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: '/login' })
}

export default router
